/**
 * Project: topdt-jbpm
 * Copyright 2013  Top Digital Technology  Co. Ltd.
 * All right reserved.
 */
package com.topdt.jbpm.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import com.thx.common.orm.HibernateBaseDao;
import com.thx.common.util.AppContext;
import com.topdt.jbpm.Constants;
import com.topdt.jbpm.model.Activity;

/**
 * 建立日期 : 2013-4-25 下午12:02:01<br>
 * 作者 : balance<br>
 * 模块 : <br>
 * 描述 : <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 * 1 <br>
 * 2 <br>
 */
@Repository
public class ActivityDao extends HibernateBaseDao<Activity> {

	public Activity getActivityByKey(String flowKey, String activityName) {

//		String comId = (String) this.getCondition(Constants.COMID_KEY);
		String comId = null;
		String condition = "";
		if (comId != null && !"".equals(comId))
			condition = "companyId='" + comId + "' and ";

		String hql = "from Activity where " + condition + " flowKey=? and activityName=?";

		List<Activity> list = createQuery(hql, new Object[] { flowKey, activityName }).list();
		if (list.size() > 0)
			return list.get(0);

		return null;
	}

	public Activity getActivityById(String flowId, String activityName) {

//		String comId = (String) this.getCondition(Constants.COMID_KEY);
		String comId = null;
		String condition = "";
		if (comId != null && !"".equals(comId))
			condition = "companyId='" + comId + "' and ";

		String hql = "from Activity where " + condition + " flowId=? and activityName=?";
		List<Activity> list = createQuery(hql, new Object[] { flowId, activityName }).list();
		if (list.size() > 0)
			return list.get(0);

		return null;
	}

	public List<Activity> getActivityByAssigner(String flowId, String activityName) {
//		String comId = (String) this.getCondition(Constants.COMID_KEY);
		String comId = null;
		String condition = "";
		if (comId != null && !"".equals(comId))
			condition = "companyId='" + comId + "' and ";
		String hql = "from Activity where "
				+ condition
				+ " flowId=? and activityName<>? and (assignerActivity=? or assignerActivity like:actName1 or assignerActivity like:actName2 or assignerActivity like:actName3)";
		List<Activity> list = createQuery(hql, new Object[] { flowId, activityName, activityName })
				.setParameter("actName1", activityName + ",%").setParameter("actName2", "%," + activityName + ",%")
				.setParameter("actName3", "%," + activityName).list();
		return list;
	}

	public List<Activity> getActivitiesByFlowId(String flowId) {

//		String comId = (String) this.getCondition(Constants.COMID_KEY);
		String comId = null;
		String condition = "";
		if (comId != null && !"".equals(comId))
			condition = "companyId='" + comId + "' and ";

		String hql = "from Activity where " + condition + " flowId=? ";
		List<Activity> list = createQuery(hql, flowId).list();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Activity> getActivityByFlow(String flowId, String flowKey) {

//		String comId = (String) this.getCondition(Constants.COMID_KEY);
		String comId = null;
		String condition = "";
		if (comId != null && !"".equals(comId))
			condition = " companyId='" + comId + "' and ";

		String hql = "from Activity t where " + condition + "t.flowId = '" + flowId + "' and t.flowKey = '" + flowKey
				+ "' order by t.seqNUM";
		List<Activity> listA = createQuery(hql).list();
		if (listA.size() > 0) {
			return listA;
		}
		return null;
	}
	/**
	 * 倒序取得环节列表在表单中显示，众至诚个性需求
	 * @author zhangyong
	 * @date   2014-10-26 下午08:21:36 
	 * @param flowId
	 * @param flowKey
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Activity> getActivityByFlowDesc(String flowId, String flowKey) {

//		String comId = (String) this.getCondition(Constants.COMID_KEY);
		String comId = null;
		String condition = "";
		if (comId != null && !"".equals(comId))
			condition = " companyId='" + comId + "' and ";

		String hql = "from Activity t where " + condition + "t.flowId = '" + flowId + "' and t.flowKey = '" + flowKey
				+ "' order by t.seqNUM desc";
		List<Activity> listA = createQuery(hql).list();
		if (listA.size() > 0) {
			return listA;
		}
		return null;
	}	
	
	
	public List<Activity> findByFlowDetailId(String flowDetailId) {
		String comId = AppContext.getCurrentComId();
		String hql = "from Activity a where a.companyId = ? and a.flowDetailId = ?";
		return createQuery(hql, new Object[]{comId, flowDetailId}).list();
	}

	public Activity getById(String activityId) {
//		String comId = (String) this.getCondition(Constants.COMID_KEY);
		String comId = null;
		String condition = "";
		if (comId != null && !"".equals(comId))
			condition = "companyId='" + comId + "' and ";

		String hql = "from Activity a where " + condition + " a.activityId = ?";
		List<Activity> list = createQuery(hql, new Object[] { activityId }).list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;

	}

	public void addActivity(String companyId) throws HibernateException{
		String sql = "insert into wf_activity w (w.ACTIVITY_ID,w.FLOW_ID,w.FLOW_KEY,w.ACTIVITY_NAME,w.ACTOR_NUM,w.DO_RULE,w.ASSIGNER_ACTIVITY,w.ASSIGNMENT_PARAM_KEY,w.ACTOR_MODEL,w.SEQ_NUM,w.COMPANY_ID,w.FLOW_DETAIL_ID) " +
				" select :companyId||wf.ACTIVITY_ID,wf.FLOW_ID,wf.FLOW_KEY,wf.ACTIVITY_NAME,wf.ACTOR_NUM,wf.DO_RULE,wf.ASSIGNER_ACTIVITY,wf.ASSIGNMENT_PARAM_KEY,wf.ACTOR_MODEL,wf.SEQ_NUM,:companyId,:companyId||wf.FLOW_DETAIL_ID " +
				" from sys_wf_activity wf";
		SQLQuery sqlQ = (SQLQuery) createSQLQuery(sql).setString("companyId", companyId);
		sqlQ.executeUpdate();
	}
}
